package com.module.content.ui.info.adapter

import androidx.recyclerview.widget.RecyclerView
import com.allen.app.data.bean.InfoBean
import com.allen.app.data.bean.ProviderBean
import com.allen.base.base.recycler.decorate.GridItemSpaceDecoration
import com.allen.base.utils.LayoutManagerUtil
import com.allen.base.utils.ViewUtils
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.module.content.R

/**
 * author : Allen
 * date   : 2020/08/14
 * time   : 13:41
 * desc   : 糗事 图片适配器
 */
class InfoMultiAdapter : InfoBaseAdapter() {

    override val itemViewType: Int = ProviderBean.PROVIDER_INFO_MULTI

    override fun getContentView(): Int = R.layout.info_adapter_info_multi

    override fun convertChild(helper: BaseViewHolder,adapterPosition: Int, item: InfoBean) {
        if (item.attachments?.isNotEmpty() == true) {
            //多张图片或gif显示
            val imageRecyclerView = helper.getView<RecyclerView>(R.id.imageRecyclerView)
            var gSize = 3
            if (item.attachments?.size == 4 || item.attachments?.size == 2) {
                gSize = 2
            }else if(item.attachments?.size == 1){
                gSize = 1
            }
            val mAdapter = InfoImageItemAdapter(gSize)
            imageRecyclerView.layoutManager = LayoutManagerUtil.getGridLayoutManager(context, gSize)
            imageRecyclerView.addItemDecoration(GridItemSpaceDecoration(gSize, ViewUtils.dp2px(5),startAndEndSpace = 0))
            imageRecyclerView.isNestedScrollingEnabled = false
            imageRecyclerView.adapter = mAdapter
            item.attachments?.let { mAdapter.addData(it) }
        }
    }
}